package java_DesignPattern.CreatorPattern.PrototypePattern.DeepClone;

import java.io.*;

/**
 * 深克隆--序列化方式
 */
public class Test_deep {

    public static void main(String[] args) {

        JZh jZh = new JZh();

        Student stu = new Student();
        stu.setName("zsj");
        jZh.setStu(stu);

        //获取对象序列化流
        try {
            //将jZh对象写入到文件中
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("jZh.txt"));
            oos.writeObject(jZh);
            oos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //从文件中读取jZh对象
        JZh jZh1 = null;
        try {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("jZh.txt"));
            jZh1 = (JZh) ois.readObject();
            //修改stu对象的name属性
            jZh1.getStu().setName("bjj");
            ois.close();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }

        //测试深克隆是否成功
        System.out.println(jZh1.getStu() == jZh.getStu());
        //show方法中打印的是stu对象的name属性,所以不会出现问题
        jZh.show();
        jZh1.show();
    }
}
